package org.spring.springboot.app.adv.advmobilemaininfo.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.adv.advarea.domain.po.AdvAreaPO;
import org.spring.springboot.app.adv.advarea.service.IAdvAreaService;
import org.spring.springboot.app.adv.advmobilemaininfo.domain.po.AdvMobileMainInfoPO;
import org.spring.springboot.app.adv.advmobilemaininfo.domain.vo.*;
import org.spring.springboot.app.adv.advmobilemaininfo.service.IAdvMobileMainInfoService;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.base.User;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(description="手机屏保广告操作接口",tags = ApiIndex.AdvMobileMainInfoController)
@RequestMapping(value = "/api/advMobileMainInfo")
@RestController
@AccessTokenVerify
public class AdvMobileMainInfoController {

    @Autowired
    private IAdvMobileMainInfoService advMobileMainInfoService;
    @Autowired
    private IAdvAreaService advAreaService;

    @ApiOperation(value = "查询分页列表")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<PageInfo<List<AdvMobileMainInfoQueryListVO>>> findPageForMyPublic(
            @ApiIgnore User user,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(value = "查询类型 1我发布的 2系统", required = true) @RequestParam(defaultValue = "1") int queryType,
            @ApiParam(value = "标题", required = false) @RequestParam(defaultValue = "") String title,
            @ApiParam(value = "代理商ID", required = false) @RequestParam(defaultValue = "") String officeId
    ) {
        R result = new R();
        AdvMobileMainInfoSearchVO searchVO = new AdvMobileMainInfoSearchVO();
        if(StringUtils.isBlank(officeId)){
            officeId=user.getOfficeId();
        }
        searchVO.setOfficeId(officeId);
        searchVO.setTitle(title);
        searchVO.setQueryType(queryType);
        List<AdvMobileMainInfoPO> list = advMobileMainInfoService.findPage(searchVO, pageNum, pageSize);
        PageInfo p = new PageInfo(list);
        p.setList(getVOS(p.getList()));
        result.setData(p);
        return result;
    }

    private List<AdvMobileMainInfoQueryListVO> getVOS(List<AdvMobileMainInfoPO> _list) {
        List<AdvMobileMainInfoQueryListVO> list = new ArrayList<AdvMobileMainInfoQueryListVO>();
        if (_list != null && _list.size()>0) {
            for (AdvMobileMainInfoPO po : _list) {
                AdvMobileMainInfoQueryListVO vo = new AdvMobileMainInfoQueryListVO();
                BeanUtils.copyProperties(po,vo);
                list.add(vo);
            }
        }
        return list;
    }

    @ApiOperation(value = "查询单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<AdvMobileMainInfoQueryDetailVO> find(
            @ApiParam(value = "id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        AdvMobileMainInfoPO po = advMobileMainInfoService.find(id);
        AdvMobileMainInfoQueryDetailVO vo = new AdvMobileMainInfoQueryDetailVO();
        BeanUtils.copyProperties(po,vo);
        //查询关联的区域
        List<AdvAreaPO> areaList = advAreaService.queryAreaList(vo.getId());
        String areaIds = "";
        if (areaList != null) {
            for (AdvAreaPO hard : areaList) {
                if(StringUtils.isNotBlank(areaIds)){
                    areaIds += ",";
                }
                areaIds += hard.getAreaId();
            }
        }
        vo.setAreaIds(areaIds);
        result.setData(vo);
        return result;
    }


    @ApiOperation(value="删除信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R delete(@ApiIgnore User user,@ApiParam(value = "id", required = true)  @PathVariable("id") String id) {
        R result = new R();
        if (StringUtils.isBlank(id)) {
            result.setType(Type.ParamValidateFail);
            result.setMessage("id不能为空");
            return result;
        }
        return advMobileMainInfoService.delete(id);
    }


    @ApiOperation(value="添加信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R<AdvMobileMainInfoSaveOutVO> save(@ApiIgnore User user, @ApiParam(value = "对象", required = true) @RequestBody AdvMobileMainInfoSaveInVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return advMobileMainInfoService.saveInfo(vo,user);
    }

    @ApiOperation(value="添加信息-系统")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @RequestMapping(value = "/system_public", method = RequestMethod.POST)
    public R saveForSystem(@ApiIgnore User user, @ApiParam(value = "对象", required = true) @RequestBody AdvMobileMainInfoSystemSaveInVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return advMobileMainInfoService.saveSystemInfo(vo,user);
    }

    @ApiOperation(value="修改信息",notes = "只能修改未审核的广告")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R update(@ApiIgnore User user, @ApiParam(value = "对象", required = true) @Valid  @RequestBody AdvMobileMainInfoUpdateInVO vo, BindingResult bindingResult) {
        R result = new R();
        if (bindingResult.hasErrors()) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        return advMobileMainInfoService.updateInfo(vo);
    }


    @ApiOperation(value = "支付广告")
    @RequestMapping(value = "/pay", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R pay(
            @ApiIgnore User user,
            @ApiParam(value = "广告id", required = false) @RequestParam(defaultValue = "") String id
    ) {
        R result = new R();
        if (StringUtils.isBlank(id)) {
            result.setType(Type.ParamValidateFail);
            result.setMessage("id不能为空");
            return result;
        }
        return advMobileMainInfoService.pay(id,user);
    }

}
